home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / 42 ƒ / 42_plugin_life.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  4.4 KB  |  245 lines  |  [TEXT/CWIE]

  1. #include "42.h"
  2. #include "42_plugin_manager.h"
  3. #include "42_plugin_life.h"
  4.  
  5. #include "LifeModule.h"
  6. #include "G4Monitor.h"
  7.  
  8. /* plugin definition */
  9.  
  10. forty_two_plugin_api    plugin_life =
  11. {
  12.     life_init,        // init
  13.     life_kill,        // kill
  14.     life_idle,        // idle1
  15.     nil,            // idle2
  16.     life_click,        // click
  17.     life_ipc,        // ipc
  18.     nil,            // update
  19.     1,            // req_dialog_item
  20.     (GWorldPtr) 0L        // port
  21. };
  22.  
  23. /* plugin functions */
  24.  
  25. unsigned long    *life_base;
  26. Handle         ThePrivateData;
  27. unsigned long    life_gen = 0;
  28. unsigned long    life_generation = 0;
  29.  
  30. int life_init(void)
  31. {
  32.     int    x,y;
  33.     long    rowbytes;
  34.         
  35.     G4SetMMCR0( kMMCR0_PMC1_processor_cycles, 0, 0);
  36.     
  37.     life_base = (unsigned long *) (**(plugin_life.port->portPixMap)).baseAddr;
  38.     rowbytes = (**(plugin_life.port->portPixMap)).rowBytes & 0x3fff;
  39.  
  40.     ThePrivateData = NewHandle(0);
  41.     PICreateLife(ThePrivateData, life_base, rowbytes, 0);
  42.  
  43.     for(x = 0; x < 128; x++)
  44.         for(y = 0; y < 128; y++)
  45.             PISetACell(ThePrivateData,x, y, 0);
  46.     
  47.     PIDrawBoard(ThePrivateData);
  48.     
  49.     return GetHandleSize(ThePrivateData);
  50. }
  51.  
  52. extern void SetTextBox(short item, unsigned char *str);
  53.  
  54. int life_idle(void)
  55. {
  56.     long        lg, ld;
  57.     unsigned char    lgs[32],lds[32];
  58.     unsigned char    gs[32];
  59.     
  60.     if(life_gen)
  61.     {
  62.         lg = life_generate();
  63.         ld = life_draw();
  64.         life_generation++;
  65.         life_gen--;
  66.         
  67.         NumToString(lg,lgs);
  68.         NumToString(ld,lds);
  69.         NumToString(life_generation,gs);
  70.         
  71.         SetTextBox(cycles_generate,lgs);
  72.         SetTextBox(cycles_draw,lds);
  73.         SetTextBox(generations,gs);
  74.     }
  75. }
  76.  
  77. int life_generate(void)
  78. {
  79.     signed long cycles, base, junk1;
  80.     
  81.     // Load timming routines into cache
  82.     G4ClearPMC();
  83.     G4GetPMC( &cycles, &junk1, &junk1, &junk1);
  84.     
  85.     G4ClearPMC();
  86.     G4GetPMC( &base,   &junk1, &junk1, &junk1);
  87.     
  88.     G4ClearPMC();
  89.     G4GetPMC( &base, &junk1, &junk1, &junk1);
  90.     
  91.     // Measure the PIGeneeration routine
  92.     G4ClearPMC();
  93.     
  94.     PINextGeneration(ThePrivateData);    
  95.     
  96.     G4GetPMC( &cycles, &junk1, &junk1, &junk1);
  97.     // subtract out the zero valuse
  98.     
  99.     cycles -= base;
  100.     
  101.     return (int)cycles;
  102. }
  103.  
  104. int life_draw(void)
  105. {
  106.     signed long cycles, base, junk1;
  107.     
  108.     // Load timming routines into cache
  109.     G4ClearPMC();
  110.     G4GetPMC( &cycles, &junk1, &junk1, &junk1);
  111.     
  112.     G4ClearPMC();
  113.     G4GetPMC( &base,   &junk1, &junk1, &junk1);
  114.     
  115.     G4ClearPMC();
  116.     G4GetPMC( &base, &junk1, &junk1, &junk1);
  117.     
  118.     // Measure the PIGeneeration routine
  119.     G4ClearPMC();
  120.     
  121.     PIDrawBoard(ThePrivateData);
  122.     
  123.     G4GetPMC( &cycles, &junk1, &junk1, &junk1);
  124.     // subtract out the zero valuse
  125.     
  126.     cycles -= base;
  127.     
  128.     (*plugin_life.update)(&plugin_life);
  129.     
  130.     return (int)cycles;
  131. }
  132.  
  133. int life_click(Point *localpt)
  134. {
  135.     Boolean    cell;
  136.     
  137.     cell = PIReadACell(ThePrivateData,localpt->h,localpt->v);
  138.     PISetACell(ThePrivateData,localpt->h,localpt->v,!cell);
  139.     PIDrawBoard(ThePrivateData);
  140.     plugin_life.update(&plugin_life);
  141. }
  142.  
  143. int life_kill(void)
  144. {
  145.     PITrashLife(ThePrivateData);
  146. }
  147.  
  148. int life_ipc(int sel, unsigned long msg)
  149. {
  150.     switch(sel)
  151.     {
  152.         case LIFE_SETCOLOR:
  153.             life_setcolor(msg);
  154.             break;
  155.         case LIFE_LOAD:
  156.             life_load(msg);
  157.             life_generation = 0;
  158.             break;
  159.         case LIFE_START:
  160.             life_gen = 99999;
  161.             break;
  162.         case LIFE_STOP:
  163.             life_gen = 0;
  164.             break;
  165.         case LIFE_STEP:
  166.             life_gen = 1;
  167.             break;
  168.     }
  169. }
  170.  
  171. void life_setcolor(int color)
  172. {
  173.     PISetColor(ThePrivateData, color);
  174.     PIDrawBoard(ThePrivateData);
  175.     (*plugin_life.update)(&plugin_life);
  176. }
  177.  
  178. void life_load(int sel)
  179. {
  180.     OSErr    err;
  181.     long    x,xx,y,yy;
  182.     short    refNum;
  183.     FSSpec    theSpec;
  184.     long     bytes;
  185.  
  186.     
  187.     switch (sel)
  188.     {
  189.         case 1:
  190.             err = FSMakeFSSpec(0,0,"\p42a.life",&theSpec);
  191.             if (err != noErr) return;
  192.             break;    
  193.         case 2:
  194.             err = FSMakeFSSpec(0,0,"\p42b.life",&theSpec);
  195.             if (err != noErr) return;
  196.             break;    
  197.         case 3:
  198.             err = FSMakeFSSpec(0,0,"\p42c.life",&theSpec);
  199.             if (err != noErr) return;
  200.             break;    
  201.         case 4:
  202.             err = FSMakeFSSpec(0,0,"\p42d.life",&theSpec);
  203.             if (err != noErr) return;
  204.             break;
  205.         case 5:
  206.             for(x = 0; x < 128; x++)
  207.                 for(y = 0; y < 128; y++)
  208.                     PISetACell(ThePrivateData,x, y, Random() % 2);
  209.             PIDrawBoard(ThePrivateData);
  210.             (*plugin_life.update)(&plugin_life);
  211.             return;
  212.             break;
  213.         default:
  214.             return;
  215.     }
  216.  
  217.     err = FSpOpenDF(&theSpec, fsRdPerm, &refNum);
  218.     if (err != noErr) return;
  219.  
  220.     bytes = 4;
  221.     FSRead(refNum, &bytes, &y);
  222.     bytes = 4;
  223.     FSRead(refNum, &bytes, &x);
  224.  
  225.     if (x != 128)
  226.         return;
  227.         
  228.     if (y != 128)
  229.         return;
  230.             
  231.     // set cells
  232.     for (xx = 0; xx < x; xx++)
  233.     {
  234.         for (yy = 0; yy < y; yy++)
  235.         {
  236.             char    state;
  237.             bytes = 1;
  238.             FSRead(refNum, &bytes, &state);
  239.             PISetACell(ThePrivateData,xx, yy,state);
  240.         }
  241.     }
  242.     FSClose(refNum);
  243.     PIDrawBoard(ThePrivateData);
  244.     (*plugin_life.update)(&plugin_life);
  245. }